package com.lf.sp.controller.portal;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lf.sp.common.Const;
import com.lf.sp.common.ResponseCode;
import com.lf.sp.common.ServerResponse;
import com.lf.sp.pojo.Shipping;
import com.lf.sp.pojo.User;
import com.lf.sp.service.IShippingService;

@Controller
@RequestMapping(value="/shipping/")
public class ShippingController {

	@Autowired
	private IShippingService iShippingService;
	/**
	 * 新增地址
	 * @param session
	 * @param shipping
	 * @return
	 */
	@RequestMapping(value="add.do")
	@ResponseBody
	public ServerResponse add(HttpSession session,Shipping shipping){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user == null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
		}
		return iShippingService.add(user.getId(), shipping);
	}
	/**
	 * 删除地址
	 * @param session
	 * @param shippingId
	 * @return
	 */
	@RequestMapping(value="del.do")
	@ResponseBody
	public ServerResponse del(HttpSession session ,Integer shippingId){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if( user == null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
		}
		return iShippingService.del(user.getId(), shippingId);
	}
	/**
	 * 更新地址
	 * @param session
	 * @param shipping
	 * @return
	 */
	@RequestMapping(value="update.do")
	@ResponseBody
	public ServerResponse update(HttpSession session,Shipping shipping){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user == null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
		}
		return iShippingService.update(user.getId(), shipping);
	}
	/**
	 * 查看地址详细信息
	 * @param session
	 * @param shippingId
	 * @return
	 */
	@RequestMapping(value="select.do")
	@ResponseBody
	public ServerResponse select(HttpSession session,Integer shippingId){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user == null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
		}
		return iShippingService.select(user.getId(), shippingId);
	}
	/**
	 * 分页查询地址
	 * @param session
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="list.do")
	@ResponseBody
	public ServerResponse list(HttpSession session ,
			@RequestParam(value="pageNum",defaultValue="1")Integer pageNum,
			@RequestParam(value="pageSize",defaultValue="10")Integer pageSize){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user == null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
		}
		return iShippingService.list(user.getId(), pageNum, pageSize);		
	}
}
